﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace KPI.Controls
{
    /// <summary>
    /// 分页控件
    /// </summary>
    public partial class PageingControl : System.Web.UI.UserControl
    {
        //分页事件
        public event EventHandler pageIndexchange;

        //每页条数
        public int PageSize
        {
            get { return Convert.ToInt32(ViewState["PageSize"]); }
            set { ViewState["PageSize"] = value; }
        }
        //当前页
        public int PageIndex
        {
            get { return Convert.ToInt32(ViewState["PageIndex"]); }
            set { ViewState["PageIndex"] = value; }
        }
        //总页数
        public int PageCount
        {
            get { return Convert.ToInt32(ViewState["PageCount"]); }
            set { ViewState["PageCount"] = value; }
        }
        //总条数
        public int rowcount
        {
            get { return Convert.ToInt32(ViewState["rowcount"]); }
            set { ViewState["rowcount"] = value; }

        }






        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                PageIndex = 1;
                lTotalRecords.Text = "总记录: " + rowcount;
                lPageCount.Text = PageCount.ToString();
            }
        }
        //首页
        //protected void btnIndex_Click(object sender, EventArgs e)
        //{
        //    PageIndex = 0;
        //    if (pageIndexchange != null)
        //    {
        //        pageIndexchange(sender, e);
        //    }
        //}
        ////尾页
        //protected void btnend_Click(object sender, EventArgs e)
        //{

        //    PageIndex = PageCount - 1;
        //    if (pageIndexchange != null)
        //    {
        //        pageIndexchange(sender, e);
        //    }
        //}
        //上一页
        protected void btnpre_Click(object sender, EventArgs e)
        {
            if (PageIndex > 1)
            {
                PageIndex--;
                if (pageIndexchange != null)
                {
                    lPageIndex.Text = PageIndex.ToString();
                    pageIndexchange(sender, e);
                }
            }
        }
        //下一页
        protected void btnnext_Click(object sender, EventArgs e)
        {
            if (PageIndex < PageCount)
            {
                PageIndex++;
                if (pageIndexchange != null)
                {
                    lPageIndex.Text = PageIndex.ToString();
                    pageIndexchange(sender, e);
                }
            }
        }
        //跳转页
        protected void lbtnGotoPage_Click(object sender, EventArgs e)
        {
            int ym=1;
            string pattern = @"^/d+$";
            if(txtGotoPage.Text.Trim()!="")
            {
                if(!Regex.IsMatch(txtGotoPage.Text.Trim(),pattern))
                {
                  ym=1;
                }
                else
                {
                    ym = Convert.ToInt32(txtGotoPage.Text.Trim());
                }
            }
         
            if (ym > 0 && ym <= PageCount) 
            {
                if (pageIndexchange != null)
                {
                    PageIndex = ym;
                    pageIndexchange(sender, e);
                }
            }
        }



    }
}